﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace BitPortal.Utils
{
    public static class URLUtils
    {
        /// <summary>
        /// Them bien so vao url trang hien tai, tra ve url
        /// </summary>
        /// <param name="param"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AddParamToQueryString(string param, string value)
        {
            string currentUrl = HttpContext.Current.Request.Url.LocalPath;
            var collection = HttpContext.Current.Request.QueryString;
            if (collection.Count > 0)
            {
                string query = "";
                bool exist = false;
                foreach (string key in collection.Keys)
                {
                    if (key.Equals(param, StringComparison.OrdinalIgnoreCase))
                    {
                        query += "&" + param + "=" + value;
                        exist = true;
                    }
                    else
                    {
                        query += "&" + key + "=" + collection[key];
                    }
                }
                if (!exist)
                {
                    query += "&" + param + "=" + value;
                }
                return HttpUtility.UrlDecode(currentUrl + "?" + query.Substring(1));
            }
            return HttpUtility.UrlDecode(currentUrl + "?" + param + "=" + value);
        }

        public static string AddParamToQueryString(Dictionary<string, string> listparam)
        {
            string currentUrl = HttpContext.Current.Request.Url.LocalPath;
            var collection = HttpContext.Current.Request.QueryString;
            foreach (string key in collection.Keys)
            {
                if (!listparam.ContainsKey(key))
                {
                    listparam.Add(key, collection[key]);
                }
            }
            string query = "";
            foreach (string param in listparam.Keys)
            {
                query += "&" + param + "=" + listparam[param];
            }
            return HttpUtility.UrlDecode(currentUrl + "?" + query.Substring(1));

        }
    }
}
